package com.atguigu.controller;


import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.util.ValidateCodeUtils;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("userInfo")
public class UserInfoController {
    @Reference
    private UserInfoService userInfoService;


    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo, HttpServletRequest request){
        //1.先判断是否为空
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)){
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //2.根据手机号获取用户信息
        UserInfo userInfo = userInfoService.getByIphone(phone);
        System.out.println(userInfo);
        if(userInfo==null){
            return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        }
        //3.判断密码是否正确
        if(!userInfo.getPassword().equals(MD5.encrypt(password))){
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
        //4.判断账户是否被锁定
        if(userInfo.getStatus()!=1){
            return Result.build(null, ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        request.getSession().setAttribute("USER",userInfo);
        Map<String,String> map = new HashMap<>();
        map.put("nickName", userInfo.getNickName());
        map.put("phone",userInfo.getPhone());
        return Result.ok(map);
    }

    @GetMapping("logout")
    public Result logout(HttpServletRequest request){
        request.getSession().invalidate(); //不仅removeAttribute("USER")，还remove了其他的Attribute，连整个session都失效了
        return Result.ok();
    }

    @PostMapping("register")
    public Result register(@RequestBody RegisterVo registerVo,HttpServletRequest request){
        String nickName = registerVo.getNickName();
        String phone = registerVo.getPhone();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        //判断表单项均不能为空（null ""）
        if(StringUtils.isEmpty(nickName)
                || StringUtils.isEmpty(phone)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code)){
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //判断验证码是否正确
        if(!code.equals(request.getSession().getAttribute("CODE"))){
            return Result.build(null,ResultCodeEnum.CODE_ERROR);
        }
        //判断数据库中是否已存在
        UserInfo userInfo = userInfoService.getByIphone(phone);
        if(userInfo!=null){
            return Result.build(null,ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        userInfo = new UserInfo();
        userInfo.setNickName(nickName);
        userInfo.setPassword(MD5.encrypt(password));
        userInfo.setPhone(phone);
        userInfo.setStatus(1);
        userInfoService.insert(userInfo);
        return Result.ok();
    }

    @GetMapping("sendCode/{phone}")
    public Result sendCode(@PathVariable String phone,HttpServletRequest request){
        String code = ValidateCodeUtils.generateValidateCode4String(6);
        request.getSession().setAttribute("CODE",code);
        //使用阿里短信发给指定的手机号码
        return Result.ok(code);
    }
}
